package com.rocket.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.rocket.R;
import com.rocket.database.WorkStationSql;
import com.rocket.entity.FirearmType;
import com.rocket.entity.WorkStation;
import com.rocket.util.DialogUtil;
import com.rocket.util.MenuUtil;

public class HistoryQueryActivity extends MenuActivity {
	Button buttonBack;
	Button buttonQuery;

	EditText etName;
	Button btnName;

	Button btnStartTime;
	Button btnEndTime;
	
	Button btnClearStartTime;
	Button btnClearEndTime;
	
	CheckBox checkboxMissile83;
	CheckBox checkboxMissileJD89;
	CheckBox checkboxRocketBL1;
	CheckBox checkboxRocketHJD82;
	CheckBox checkboxRocketRYI6300;
	CheckBox checkboxRocketWR98;

	ArrayList<WorkStation> mStations;
	WorkStation mStation;

	AlertDialog dialog;

	Date startTime = new Date();
	Date endTime = new Date();
	
	long start = 0;
	long end = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.historyquery);

		buttonBack = (Button) findViewById(R.id.buttonback);
		buttonBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		buttonQuery = (Button) findViewById(R.id.buttonquery);
		buttonQuery.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(start != 0 && end ==0){
					showToast("请选择结束时间");
					return;
				}
				if(start > end){
					showToast("起始时间不能大于结束时间，请重新设置时间");
					return;
				}
				String name = etName.getText().toString().trim();
				ArrayList<Integer> types = new ArrayList<Integer>();
				if(checkboxMissile83.isChecked()){
					types.add(FirearmType.MISSILE_83);
				}
				if(checkboxMissileJD89.isChecked()){
					types.add(FirearmType.MISSILE_JD89);
				}
				if(checkboxRocketBL1.isChecked()){
					types.add(FirearmType.ROCKET_BL1);
				}
				if(checkboxRocketHJD82.isChecked()){
					types.add(FirearmType.ROCKET_HJD82);
				}
				if(checkboxRocketRYI6300.isChecked()){
					types.add(FirearmType.ROCKET_RY6300);
				}
				if(checkboxRocketWR98.isChecked()){
					types.add(FirearmType.ROCKET_WR98);
				}
				
				Intent intent = new Intent(HistoryQueryActivity.this,HistoryListActivity.class);
				intent.putExtra("name", name);
				intent.putExtra("start", start);
				intent.putExtra("end", end);
				intent.putExtra("types", types);
				startActivity(intent);
				
			}
		});

		etName = (EditText) findViewById(R.id.etname);
		btnName = (Button) findViewById(R.id.btnname);
		btnName.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ArrayAdapter<String> nameAdapter = new ArrayAdapter<String>(
						HistoryQueryActivity.this,
						android.R.layout.simple_list_item_single_choice);
				mStations = WorkStationSql.getInstance().query();
				for (WorkStation station : mStations) {
					nameAdapter.add(station.name);
				}
				ListView listview = new ListView(HistoryQueryActivity.this);
				listview.setAdapter(nameAdapter);
				listview.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						if (dialog != null) {
							dialog.dismiss();
						}
						mStation = mStations.get(arg2);
						etName.setText(mStation.name);
					}
				});
				dialog = new AlertDialog.Builder(HistoryQueryActivity.this)
						.setTitle("选择站点").setView(listview).show();

			}
		});

		btnStartTime = (Button) findViewById(R.id.btnstarttime);
		btnStartTime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DatePickerDialog dialog = new DatePickerDialog(
						HistoryQueryActivity.this, new OnDateSetListener() {

							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								startTime.setYear(year - 1900);
								startTime.setMonth(monthOfYear);
								startTime.setDate(dayOfMonth);
								startTime.setHours(0);
								startTime.setMinutes(0);
								startTime.setSeconds(0);
								SimpleDateFormat format = new SimpleDateFormat(
										"yyyy年MM月dd日");
								btnStartTime.setText(format.format(startTime));
								start = startTime.getTime()/100000*100000;
							}
						}, startTime.getYear()+1900, startTime.getMonth(), startTime
								.getDate());
				dialog.show();
			}
		});
		btnEndTime = (Button) findViewById(R.id.btnendtime);
		btnEndTime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DatePickerDialog dialog = new DatePickerDialog(
						HistoryQueryActivity.this, new OnDateSetListener() {

							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								endTime.setYear(year- 1900);
								endTime.setMonth(monthOfYear);
								endTime.setDate(dayOfMonth);
								endTime.setHours(0);
								endTime.setMinutes(0);
								endTime.setSeconds(0);
								SimpleDateFormat format = new SimpleDateFormat(
										"yyyy年MM月dd日");
								btnEndTime.setText(format.format(endTime));
								end = endTime.getTime()/100000*100000;
							}
						}, endTime.getYear()+1900, endTime.getMonth(), endTime
								.getDate());
				dialog.show();
			}
		});
		
		btnClearStartTime = (Button) findViewById(R.id.btnclearstart);
		btnClearStartTime.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				btnStartTime.setText("请选择起始时间");
				startTime = new Date();
				start = 0;
			}
		});
		btnClearEndTime = (Button) findViewById(R.id.btnclearend);
		btnClearEndTime.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				btnEndTime.setText("请选择结束时间");
				endTime = new Date();
				end = 0;
			}
		});

		checkboxMissile83 = (CheckBox) findViewById(R.id.checkmissile_83);
		checkboxMissileJD89 = (CheckBox) findViewById(R.id.checkmissile_jd89);
		checkboxRocketBL1 = (CheckBox) findViewById(R.id.checkrocket_bl1);
		checkboxRocketHJD82 = (CheckBox) findViewById(R.id.checkrocket_hjd82);
		checkboxRocketRYI6300 = (CheckBox) findViewById(R.id.checkrocket_ryi6300);
		checkboxRocketWR98 = (CheckBox) findViewById(R.id.checkrocket_wr98);
	}

	@Override
	protected void openMenu() {
		String[] texts = new String[] { "火器选择", "添加站点", "历史查询", "退出程序" };
		Integer[] images = new Integer[] { R.drawable.menu_logout,
				R.drawable.menu_sys_add_buddy,
				R.drawable.menu_sys_search_buddy, R.drawable.menu_icon };
		mMenu = new MenuUtil(HistoryQueryActivity.this, getLayoutInflater()
				.inflate(R.layout.historyquery, null), texts, images);
		mMenu.setMenuItemSelectedLinstener(new OnClickListener() {

			public void onClick(View v) {
				int tag = (Integer) v.getTag();
				switch (tag) {
				case 0:
					mMenu.dismiss();
					Intent intent1 = new Intent(HistoryQueryActivity.this,
							ChoseFirearmActivity.class);
					intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent1);
					break;
				case 1:
					mMenu.dismiss();
					Intent intent2 = new Intent(HistoryQueryActivity.this,
							WorkStationAddActivity.class);
					intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent2);
					break;
				case 2:
					mMenu.dismiss();
					Intent intent3 = new Intent(HistoryQueryActivity.this,
							HistoryQueryActivity.class);
					intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent3);
					break;
				case 3:
					mMenu.dismiss();
					mDialog = new DialogUtil(HistoryQueryActivity.this);
					mDialog.setTitle("退出确认");
					mDialog.setMessage("确定退出吗？");
					mDialog.setPositiveButton("确定", new OnClickListener() {

						public void onClick(View v) {
							mDialog.dismiss();
							Intent intent = new Intent(
									HistoryQueryActivity.this,
									MainActivity.class);
							intent.putExtra("Flag", 1);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							startActivity(intent);
						}
					});
					mDialog.setNegativeButton("取消", null);
					mDialog.show();
					break;
				default:
					break;
				}
			}
		});
		mMenu.show();
	}

}
